'
'GCV_FRONT = 2
'GCV_LEFT = 4
'GCV_RIGHT = 5
'GCV_EXTENDED = 7
'GCV_SIMPLIFIED = 8
'GCV_STANDARD = 9
'SIDE = GCV_FRONT
'DETAIL_LEVEL =GCV_STANDARD' GCV_EXTENDED

hatchAngle  = 45                  
'hatchStep= 2                   

drawFace1 =1

'PAR_din=20
'PAR_d=47
'PAR_b=14
'PAR1_dain= (25+26) * 0.5
'PAR1_da= 42
'PAR1_dw=7.9
'PAR1_r = 0.7
'PAR2_r1 =  0.5
PAR2_tip =1

Dim math,iDA,iPoint
Const POINT_ARR = 2  
Const ko_MathPointParam = 14

''PAR_AXIS = 1
rightSide =1

'++++
SUB LINE (x1,y1,x2,y2,t) Doc.ksLineSEG x1,y1,x2,y2,t END SUB
SUB Circle (x1,y1,r,t) Doc.ksCircle x1,y1,r,t END SUB
'++++
SUB DrawBall (xtr, ytr, rads, del)
  Line  xtr-rads-del,ytr,xtr+rads+del,ytr,3
  Line  xtr,ytr+rads+del,xtr,ytr-rads-del,3
  Circle xtr,ytr,rads,1
end sub
'++++
SUB Contur (rn, rf1, rf2,rf3, rf4, b,  ky) 
 x1=rf1
 x2=rf2
 x3=b-rf3
 x4=b-rf4
 y2=(rn-rf2)*ky
 y3=(rn-rf3)*ky
  doc.ksArcByPoint x2,y2,rf2,0,y2,x2,rn*ky,-ky,1 ';  /*5*/
  line x2,rn*ky,x3,rn*ky,1
 doc.ksArcByPoint x3,y3,rf3,x3,rn*ky,b,y3,-ky,1 ' ; /*6*/
 Line 0, y2, 0,0,1
 Line b,y3,b,0,1
END SUB
'++++
SUB Apx (rn)
  doc.ksColouring &HFE000000
Contur rn, PAR1_r,PAR2_r1, PAR1_r, PAR1_r   , PAR_B, 1
Contur rn, PAR1_r,PAR2_r1, PAR1_r, PAR1_r  , PAR_B, -1
doc.ksEndObj
END SUB
'++++


SUB Script_OnLoad ()


Set math = Kompas.GetMathematic2D
Set iDA = Kompas.GetDynamicArray(POINT_ARR)
Set iPoint = Kompas.GetParamStruct(ko_MathPointParam)

 ys=(  PAR_din+ PAR_D)*0.25
 xs= PAR_B*0.5                 
 rn= PAR_D*0.5
 rv= PAR_din*0.5
 rv2=PAR1_dain*0.5
 rn2=PAR1_Da*0.5
 rsh = PAR1_Dw*0.5

  if  rv2 < (ys-0.8*rsh)  then
    rv2 = ys-0.8*rsh
  end if

if  rn2 > (ys+0.8*rsh) then
    rn2 = ys+0.8*rsh
end if

APX rn

  SELECT CASE DETAIL_LEVEL
      Case GCV_EXTENDED:
      DrawOutsideRing   rn,  rn2,  PAR_B,  PAR1_r, xs, ys, rsh, 1,  PAR2_r1, 0  
        DrawBall  xs, ys, rsh,  2
       DrawInsideRing   rv, rv2,  PAR_B,  PAR1_r, xs, ys, rsh, 1,  PAR2_r1, rightSide  
   
       IF PAR_AXIS <> 1 THEN
       	If rightSide = 1 Then 
         line   PAR_B, rn- PAR1_r,  PAR_B, rv+ PAR2_r1, 1 
         Line  0, rn- PAR2_r1, 0, rv + PAR1_r, 1
        Else 
         line  0, rn- PAR1_r, 0, rv + PAR2_r1, 1
         line   PAR_B, rn- PAR1_r,  PAR_B, rv+ PAR1_r, 1  
        End If
       end if

        DrawOutsideRing   rn,  rn2,  PAR_B,  PAR1_r, xs, ys, rsh, -1,  PAR2_r1, 0  
        DrawBall  xs, -ys,  PAR1_Dw*0.5,  2
       DrawInsideRing   rv, rv2,  PAR_B,  PAR1_r, xs, ys, rsh, -1,  PAR2_r1, rightSide  
       
        IF PAR_AXIS <> 1 THEN
         If rightSide = 1 Then 
         	  line  0, -(rn- PAR2_r1), 0, -(rv+ PAR1_r), 1 
            line   PAR_B, -(rn- PAR1_r),  PAR_B, -(rv+ PAR2_r1), 1 
         Else 
            line   PAR_B, -(rn- PAR1_r),  PAR_B, -(rv+ PAR1_r), 1 
            line  0, -(rn- PAR1_r), 0, -(rv+ PAR2_r1), 1 
         End If
        end if
      
      case  GCV_STANDARD:
        DrawOutsideRing   rn,  rn2,  PAR_B,  PAR1_r, xs, ys, rsh, 1, PAR2_r1, 0  
        DrawBall  xs, ys, rsh,  2
        DrawInsideRing   rv, rv2,  PAR_B,  PAR1_r, xs, ys, rsh, 1,  PAR2_r1, rightSide  
         IF PAR_AXIS <> 1 THEN
          line  0, rn- PAR2_r1, 0, rv+ PAR1_r, 1
          If rightSide = 1 Then 
           line   PAR_B, rn- PAR1_r,  PAR_B, rv+ PAR2_r1, 1 
           line  0, rn- PAR1_r, 0, rv + PAR1_r, 1
          Else 
           line  0, rn- PAR1_r, 0, rv + PAR2_r1, 1
           line   PAR_B, rn- PAR1_r,  PAR_B, rv+ PAR1_r, 1  
          End If
        end if
          DrawSimple  rv, rn,  PAR1_r,PAR2_r1,  PAR1_r, PAR2_r1 , PAR_B, -1 

CASE GCV_SIMPLIFIED:
        DrawSimple rv, rn,  PAR1_r,PAR2_r1,  PAR1_r,  PAR2_r1,  PAR_B, 1 
        DrawSimple rv, rn,  PAR1_r,PAR2_r1,  PAR1_r,  PAR2_r1,  PAR_B, -1 
     END SELECT
'END IF

 iDA.ksDeleteArray
END SUB
         

'++++++++++++++++++
SUB DrawOutsideRing    ( rn,   rn2,  b,  r,  xs, ys,  rads,  ky,  r1, rightSide ) 
  
'rL = rightSide ? r : r1;

if rightSide=1 then
rL = r
rR = r1
else
rL=r1
rR=r
end if
 
'rR = rightSide ? r1 : r;
'if rightSide=1 then
'rR = r1
'else
'rR=r
'end if


 yL=(rn-rL)*ky
 yR=(rn-rR)*ky
 
 
Dim  x(2)
Dim  y(2)
 
iDA.ksClearArray
 math.ksIntersectLinLin  b*0.5, (ys+rads)*ky, 15*ky, b, 0, 90*ky, iDA
I = iDA.ksGetArrayCount

If i<> 0 then
iDA.ksGetArrayItem 0, iPoint
 y(0) = iPoint.y
 x(0) = iPoint.x

if  rightSide =1  then
   yRight = y(0)
   yLeft  = rn2*ky
 
 else 
   yLeft   = y(0)
   yRight  = rn2*ky
 end if
end if

iDA.ksClearArray

if  rightSide = 1 then 
par =0
else 
par =b
end if


 math.ksIntersectLinSCir  par, rn2*ky, xs, rn2*ky, xs, ys*ky, rads, iDA

 I = iDA.ksGetArrayCount


 iDA.ksGetArrayItem 0, iPoint
 y(0) = iPoint.y
 x(0) = iPoint.x

' iDA.ksGetArrayItem 1, iPoint
'     x(1) = iPoint.x
'     y(1) = iPoint.y

  if rightSide=1 then 
    x(1) = xs
    y(1) = (ys+rads)*ky
  else 
    x(1) = x(0)
    y(1) = y(0)
    x(0) = xs
    y(0) = (ys+rads)*ky
end if  

 doc.ksArcByPoint rL,yL,rL,0,yL,rL,rn*ky,-ky,1
  line rL,rn*ky,b-rR,rn*ky,1
    doc.ksArcByPoint b-rR,yR,rR,b-rR,rn*ky,b,yR,-ky,1
  line  b, yRight, x(1), y(1), 1 
  line  x(0), y(0), 0, yLeft, 1 

  Doc.kshatch  0, hatchAngle, hatchStep, 0, 0, 0
     doc.ksArcByPoint rL,yL,rL,0,yL,rL,rn*ky,-ky,1
     line rL,rn*ky,b-rR,rn*ky,1
     doc.ksArcByPoint b-rR,yR,rR,b-rR,rn*ky,b,yR,-ky,1
     line  b,yR, b, yRight, 1 
     line  b, yRight, x(1), y(1), 1 
     doc.ksArcByPoint  xs, ys*ky, rads, x(1), y(1), x(0), y(0),  ky ,1 
     line  x(0), y(0), 0, yLeft, 1 
     line   0, yLeft, 0,yL, 1 

Doc.ksEndObj

If PAR_AXIS = 1 Then 
   line b,yR,b,0,1
   If drawFace1 = 1 Then 
     line 0,yL,0,0,1
    End If  
  if  ky = 1 then
     line  -2, 0, b+2, 0, 3 
 end if
End If 

 end sub 
'+++++
sub DrawInsideRing ( rv, rv2, b, r, xs,ys, rads, ky,  r1, rightSide ) 

if rightSide=1 then
rL = r
rR = r1
else
rL=r1
rR=r
end if

 yL=(rv+rL)*ky
 yR=(rv+rR)*ky 

  Dim  xp(2) 
  Dim  yp(2) 

  iDA.ksClearArray
math.ksIntersectLinLin  b*0.5, (ys-rads)*ky, -15*ky, b, 0, 90*ky, iDA
  I = iDA.ksGetArrayCount

 
If i<> 0 then
iDA.ksGetArrayItem 0, iPoint
 yp(0) = iPoint.y
 xp(0) = iPoint.x

if  rightSide =1  then
   yRight = yp(0)
   yLeft  = rv2*ky
 
 else 
   yLeft   = yp(0)
   yRight  = rv2*ky
 end if
end if

iDA.ksClearArray

if  rightSide = 1 then 
par =0
else 
par =b
end if


 math.ksIntersectLinSCir  par, rv2*ky, xs, rv2*ky, xs, ys*ky, rads, iDA

 I = iDA.ksGetArrayCount


 iDA.ksGetArrayItem 0, iPoint
 yp(0) = iPoint.y
 xp(0) = iPoint.x


  if rightSide=1 then 
    xp(1) = xs
    yp(1) = (ys-rads)*ky
  else 
    xp(1) = xp(0)
    yp(1) = yp(0)
    xp(0) = xs
    yp(0) = (ys-rads)*ky
end if  


   
   doc.ksArcByPoint b-rR,yR,rR,b,yR,b-rR,rv*ky,-ky,1
   line rL,rv*ky,b-rR,rv*ky,1
   doc.ksArcByPoint rL,yL,rL,rL,rv*ky,0,yL,-ky,1

   If PAR_AXIS = 1 Then  
    line rL,rv*ky,rL,0,1
    line b-rR,rv*ky,b-rR,0,1
 End If 
   line  b, yRight, xp(1), yp(1), 1 
   line  xp(0), yp(0), 0, yLeft, 1


 Doc.kshatch  0, hatchAngle-90, hatchStep, 0, 0, 0
  doc.ksArcByPoint b-rR,yR,rR,b,yR,b-rR,rv*ky,-ky,1
  If yR <> yRight Then 
  	Line b,yR,b,yRight,1
  End If 
   line rL,rv*ky,b-rR,rv*ky,1
   doc.ksArcByPoint rL,yL,rL,rL,rv*ky,0,yL,-ky,1
   line 0,yL,0,rv2*ky,1
   doc.ksArcByPoint  xs, ys*ky, rads, xp(1), yp(1), xp(0), yp(0), - ky ,1
   line  b, yRight, xp(1), yp(1), 1 
   line  xp(0), yp(0), 0, yLeft, 1
  Doc.ksEndObj

 end sub 

'++++++++
sub DrawSimple (rv, rn, rf1, rf2,rf3, rf4, b,  ky) 


 x1=rf1
 x2=rf2
 x3=b-rf3
 x4=b-rf4

 y1=(rv+rf1)*ky
 y2=(rn-rf2)*ky
 y3=(rn-rf3)*ky
 y4=(rv+rf4)*ky

    line 0,y1,0,y2,1

  doc.ksArcByPoint x2,y2,rf2,0,y2,x2,rn*ky,-ky,1 ';  /*5*/
  line x2,rn*ky,x3,rn*ky,1
  doc.ksArcByPoint x3,y3,rf3,x3,rn*ky,b,y3,-ky,1 ' ; /*6*/
  line b,y3,b,y4,1
doc.ksArcByPoint x4,y4,rf4,b,y4,x4,rv*ky,-ky,1 '; /*7*/
  line x4,rv*ky,x1,rv*ky,1
  doc.ksArcByPoint x1,y1,rf1,x1,rv*ky,0,y1,-ky,1 ';     /*8*/

 If PAR_Simpl_AXIS = 1 Then 
  Line x1,y1,x3,y3,2
  Line x2,y2,x4,y4,2
  Else 
  ''NEW 
   Line b/3,(rn+rv)*0.5*ky,2*b/3,(rn+rv)*0.5*ky,2
   Line b*0.5,((rn+rv)*0.5-b/6)*ky,b*0.5,((rn+rv)*0.5+b/6)*ky,2
   ''
  End If  
  if PAR_AXIS = 1 THEN  
    line b,0,b,y4,1
    line x1,0,x1,rv*ky,1
    line x4,0,x4,rv*ky,1
          line 0,y1,0,0,1
     if ky = 1  then
      line -2, 0, b+2, 0, 3
end if
  end if
end sub
'+++++++++++++++



